import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import WelCome from "../components/WelCome";
Vue.use(VueRouter)

const routes = [
  {path: '/', name: 'Login', component: Login},
  {path: '/Login', name:'Login',component: Login},
  {path: '/Home',
   name: 'Home',
   component: Home,
    children:[
      {path:'/',component:WelCome},
    ]}
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫 控制前台的访问
router.beforeEach((to,from,next)=>{
    let path = to.path;
    if (path == '/' || path == '/login') {
        next();
    }else {
        let userInfo = sessionStorage.getItem("loginUser");
        if (userInfo != null) {
            next();
        }else {
            alert("请先登录！");
            next("/")
        }
    }
})

export default router
